<script setup lang="ts">
import { onMounted } from 'vue';
import { navigateTo, reLaunch, showLoading } from '@tarojs/taro';
import { Right as NutIconRight } from '@nutui/icons-vue-taro';
import { QUERY_WAREHOUSE_LIST } from '/@/api/modules/spare-parts/warehouse-manage';
import { state } from './state';

/**
 * @description 消息列表查询
 * @param {Number} page -当前页
 * @param {Number} pageSize -每页条数
 */
function queryList(page: number = 1, pageSize: number = 15) {
  if (!page && state.finished) return;
  state.loading = true;
  showLoading();
  QUERY_WAREHOUSE_LIST({
    comQuery: { page, pageSize },
  })
    .then((res) => {
      state.loading = false;

      if (
        state.activePage >=
        (res.comQuery?.pageCount ||
          (res.comQuery?.totalCount as number) / (res.comQuery?.pageSize as number) ||
          1)
      )
        state.finished = true;
      if (!res.content || !res.content.length) return;

      res.content.forEach((item) => state.list.push(item));

      state.activePage += 1;
    })
    .catch(() => {
      state.loading = false;
    });
}

/**
 * @function 加载
 */
function onLoading() {
  if (state.finished) return;

  queryList(state.activePage);
}

onMounted(() => {
  state.activePage = 1;
  state.loading = true;
  state.finished = false;
  state.list = [];
  onLoading();
});
</script>

<template>
  <view class="spare-list app-page--1">
    <app-navbar title="耗材申请" />
    <nut-tabs
      model-value="record"
      @click="
        ({ paneKey }) => {
          if (paneKey === 'push') {
            reLaunch({ url: '/pages/approve/record/index?from=spare' });
          }
        }
      "
    >
      <nut-tab-pane title="发起申请" pane-key="record" />
      <nut-tab-pane title="申请记录" pane-key="push" />
    </nut-tabs>
    <scroll-view
      class="app-page__wrapper"
      :scroll-y="true"
      :lower-threshold="100"
      @scrolltolower="onLoading()"
    >
      <view class="spare-list__wrapper">
        <view
          class="spare-list-item"
          v-for="item in state.list"
          :key="item.id"
          @click="
            navigateTo({
              url: `/pages/spare/edit/index?id=${item.id}&warehouseCode=${item.warehouseCode}&warehouseName=${item.warehouseName}`,
            })
          "
        >
          <view class="spare-list-item__left">
            <view class="spare-list-item__left--top"> {{ item.warehouseName }}</view>
            <view>
              <text>{{ item.address || '-' }}</text>
            </view>
          </view>
          <view class="spare-list-item__right">
            <nut-icon-right />
          </view>
        </view>

        <view class="app-page__list-tips">
          <text v-if="!state.list.length">暂无数据！</text>
          <text v-else-if="state.list.length && state.loading">加载中...</text>
          <text v-else-if="state.list.length && state.finished">加载完成!</text>
        </view>
      </view>
    </scroll-view>
  </view>
</template>

<style lang="scss">
@import './index.scss';
</style>
